﻿using System;
using Framework.MVC;
using Log.Model;
using Config.Model;
using System.Collections.Generic;
using Framework.Common.Entry;
using Framework.Extention.Billing;
using Extention.Model;
using Extention.Model.Behavior;
using Framework.MVVM.Model;
using Common.IService;
using Common.Service;
using Log.IService;
using Log.Service;
using Config.IService;
using Config.Service;


namespace FrontCenter.Models
{
    [ServiceAttribute(typeof(Extention.IService.IOrderService), typeof(Extention.Service.OrderService), "Instance")]
    [ServiceAttribute(typeof(IConfigService), typeof(ConfigService), "Instance")]
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class EndOrderModel : AsyncModelBase, IWriteLog, IGetConfig<IDictionary<string, KeyValueEntry<string, string>>>, IGetConfig<IBilling>, IEndOrder
    {
        #region Field

        private const string m_error_type = "endorder0";
        private const string m_complete_type = "endorder1";
        private const string m_error_validate = "endorder2";
        private const string m_complete_validate = "endorder3";
        private const string m_end_error = "endorder4";
        private const string m_end_complete = "endorder5";

        private const string m_content = "content";
        private const string m_result = "result";

        private const string m_type_config = "data:order:type";

        #endregion Field

        #region Property

        public string Source { get; set; }

        #endregion Property

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            ConfigName = m_type_config;
            this.GetConfig<IDictionary<string, KeyValueEntry<string, string>>>(m_error_type, m_complete_type);
        }

        #endregion AsyncModelBase

        #region Write Log

        public void OnWriteLogComplete(string eventCode)
        {
            OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            OnEndRequest();
        }

        #endregion Write Log

        #region IGetConfig Billing Type

        public string Type { get; set; }

        public void OnGetConfigComplete(string key, IDictionary<string, KeyValueEntry<string, string>> entry)
        {
            if (entry.ContainsKey(Type))
            {
                OnBeginRequest();
                ConfigName = entry[Type].key;
                this.GetConfig<IBilling>(m_error_validate, m_complete_validate);
            }
            this.Log(key, Source);
        }

        #endregion IGetConfig Billing Type

        #region IGetConfig Billing

        public string ConfigName { get; set; }

        public KeyValueEntry<string, object>[] Params { get; set; }

        public void OnGetConfigComplete(string key, IBilling entry)
        {
            string code = string.Empty;
            decimal amount = decimal.Zero;
            if (entry.ValidateParams(out code, out amount, Params))
            {
                OnComplete(m_content, entry.Success);
                OnBeginRequest();
                OrderCode = code;
                OrderAmount = amount;
                this.EndOrder(m_end_error, m_end_complete);
            }

            this.Log(key, Source);
        }

        public void OnGetConfigError(string key)
        {
            this.Log(key, Source);
        }

        #endregion IGetConfig Billing

        #region IEndOrder

        public decimal OrderAmount { get; set; }

        public string OrderCode { get; set; }

        public string OrderSource { get; set; }

        public void OnEndOrderComplete(string key, byte status)
        {
            OnComplete(m_result, status>1);
            this.Log(key, Source);
        }

        public void OnEndOrderError(string key)
        {
            this.Log(key, Source);
        }

        #endregion IEndOrder
    }
}